package com.tarena.lbs.repository;

import com.tarena.lbs.entity.AttachFile;
import org.apache.ibatis.annotations.*;

import java.util.Date;

@Mapper
public interface AttachFileRepository {

    @Insert("INSERT INTO attach(file_uuid, client_file_name, content_length, content_type, " +
            "create_user_id, create_user_name) " +
            "VALUES(#{fileUuid}, #{clientFileName}, #{contentLength}, #{contentType}, " +
            "#{createUserId}, #{createUserName})")
//@Insert("INSERT INTO attach(file_uuid, client_file_name, content_length, content_type, " +
//        "create_user_id, gmt_create, create_user_name) " +
//        "VALUES(#{fileUuid}, #{clientFileName}, #{contentLength}, #{contentType}, " +
//        "#{createUserId}, #{gmtCreate}, #{createUserName})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(AttachFile attachFile);

    @Select("SELECT * FROM attach WHERE file_uuid = #{fileUuid}")
    AttachFile findByUuid(String fileUuid);

    @Delete("DELETE FROM attach WHERE id = #{id}")
    int delete(Long id);

    @Delete("DELETE FROM attach WHERE status = 0 AND gmt_create < #{expireDate}")
    int deleteExpiredFiles(Date expireDate);

}
